package com.ruoyi.framework.config;

import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;

public class GetSocketInfoThread extends Thread {
    static  Socket  socket;

    static {
        try {
//            socket = new Socket("192.168.0.10",2000);
            socket = new Socket("192.168.43.179",8080);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        System.out.println("准备接收plc指令");
        try {
            while (true){
                InputStream in = socket.getInputStream();
                if (in != null){
                    byte[] b = new byte[8];//接收plc的指令

                    int len = -1;
                    if ((len=in.read(b))!=-1){
                        MyWebSocket myWebSocket = new MyWebSocket();
                        StringBuffer stringBuffer = new StringBuffer();
                        for (int i =0;i<b.length;i++){
                            if (i!=b.length-1){
                            	b[0] = 2;//模式1：全自动；2：半自动 ；0：info
                            	b[1] = 1;//是否启动 0：未启动；1：启动    
                            	b[2] = 1;//几台行车 1:靠近扫描器行车；2：远离扫描器行车；3：两台行车
                            	b[3] = 3;//报错信息 M1 :左侧行车位置选取问题,M2：右侧行车位置选取问题 ，M3：没问题
                            	b[4] = 1;//放料完成 0.未完成;1.左侧行车完成；2.右侧行车完成；3.全完成
                                stringBuffer.append(b[i]);
                                stringBuffer.append(",");
                            }else{
                                stringBuffer.append(b[i]);
                            }
                        }
                        myWebSocket.sendMessage(stringBuffer.toString());
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("接收指令结束");
    }
}
